﻿namespace etracks.Helpers
{
    using etracks.EntityModels;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
using System.Web.Mvc;

    public class DataHelper
    {
        public static string IfMatch<T>(T v1, T v2, string value) where T : IEquatable<T>
        {
            if (v1.Equals(v2))
                return value;

            return null;
        }

        public static HtmlString IsSelected(string s1, string s2)
        {
            return new HtmlString(
                IfMatch<string>(s1, s2, "selected=\"selected\""));
        }

        public static HtmlString IsSelected(object obj1, object obj2)
        {
            if ((obj1 != null) && (obj2 != null))
                return new HtmlString(IfMatch<string>(obj1.ToString(), obj2.ToString(), "selected=\"selected\""));

            return null;
        }

        public static HtmlString IsDisabled(int count)
        {
            //if ((obj1 != null) && (obj2 != null))
            //    return IfMatch<string>(obj1.ToString(), obj2.ToString(), "disabled=\"disabled\"");
            if (count == 1)
                return new HtmlString("disabled=\"disabled\"");

            return null;
        }

        public static bool IsNull(object obj)
        {
            return obj == null;
        }

        public static string IfNull(object obj)
        {
            return IfNull(obj, "undefined");
        }

        public static string IfNull(object obj, string value)
        {
            if (obj == null)
                return value;

            return obj.ToString();
        }

        public static HtmlString Alert(BootstrapAlert alert)
        {
            if (alert == null)
                return null;

            string cssClass = "alert alert-danger";
            string title = "<strong>Alert&gt;</strong>";

            switch (alert.Status)
            {
                case BootstrapAlertStatus.danger:
                    cssClass = "alert alert-danger";
                    break;
                case BootstrapAlertStatus.info:
                    cssClass = "alert alert-info";
                    break;
                case BootstrapAlertStatus.success:
                    cssClass = "alert alert-success";
                    break;
                case BootstrapAlertStatus.warning:
                    cssClass = "alert alert-warning";
                    break;
                default:
                    cssClass = "alert alert-primary";
                    break;

            }
            return new HtmlString(
                string.Format(@"<div class=""{0}"" role=""alert"">
    {1} {2}
</div>", cssClass, title, alert.Message));
        }

        public static HtmlString Alert(ViewDataDictionary viewData)
        {
            BootstrapAlert alert = null;

            alert = viewData["bootstrap_alert"] as BootstrapAlert;

            if (alert == null)
                return null;

            return Alert(alert);
        }

        public static HtmlString Alert(TempDataDictionary tempData)
        {
            BootstrapAlert alert = null;

            alert = tempData["bootstrap_alert"] as BootstrapAlert;

            if (alert == null)
                return null;

            return Alert(alert);
        }

        public static HtmlString LinkButton(string url, string buttonText)
        {
            return LinkButton(url, buttonText, null);
        }

        public static HtmlString AddLinkButton(string url, string buttonText)
        {
            return LinkButton(url, buttonText, "glyphicon-plus");
        }

        public static HtmlString LinkButton(string url, string buttonText, string glyph)
        {
            string glyph_markup = string.Empty;

            if (!string.IsNullOrEmpty(glyph))
                glyph_markup = string.Format(@"<span class=""glyphicon {0}""></span> ", glyph);

            return new HtmlString(
                string.Format(@"<a href=""{0}"" class=""btn btn-default"">{1}{2}</a>",
                url,
                glyph_markup,
                buttonText));
        }
    }
}
